///
//  LogAs v 1.0
//  Russell Lowke, Febrauary 22nd 2011
//
//  Copyright (c) 2011 Lowke Media
//  see http://www.lowkemedia.com for more information
//
//  Permission is hereby granted, free of charge, to any person obtaining a 
//  copy of this software and associated documentation files (the "Software"), 
//  to deal in the Software without restriction, including without limitation 
//  the rights to use, copy, modify, merge, publish, distribute, sublicense, 
//  and/or sell copies of the Software, and to permit persons to whom the 
//  Software is furnished to do so, subject to the following conditions:
// 
//  The above copyright notice and this permission notice shall be included in 
//  all copies or substantial portions of the Software.
// 
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
//  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 
//  IN THE SOFTWARE. 
//
//

// Starting to convert my LogAs AS3 class to JavaScript

(function(window) {
    
    function LogAs()
    {    
        if (! LogAs.instance) {
            LogAs.instance = this;
        } else {
            throw new Error("LogAs is a singleton, it should never be created twice.\r" + 
                            "Use LogAs.instance to get a reference to AssetLoader.");
        }
        
        this.traceLogs = true;          // (boolean) if true logs are traced
        this.logLevel = LogAs.DEBUG;    // (uint) log level should be, NONE, SEVERE, WARNING, INFO or DEBUG
    
        this.log = function(level,      // (uint) the log level NONE, SEVERE, WARNING, INFO or DEBUG
                            message,    // (string) the message being logged
                            id)         // (int) id number of this message
        {
            if (level <= this.logLevel) {
                
                // var evt:LogAsEvent = new LogAsEvent(LOG_AS_EVENT, message, level, id);
                var evt = $.Event(LogAs.LOG_AS_EVENT);
                evt.message = message;
                evt.level = level;
                evt.id = id;
                
                if (this.traceLogs) {
                    
                    var str;
                    switch (level) {
                        case LogAs.SEVERE:    str = "SEVERE";     break;
                        case LogAs.WARNING:   str = "WARNING";    break;
                        case LogAs.INFO:      str = "INFO";       break;
                        case LogAs.DEBUG:     str = "DEBUG";      break;
                        default:
                            throw new Error("Can't understand LogAs error level of " + level);
                    }
            
                    // SHOULD TRACE MESSAGE PROPERLY HERE RATHER THAN USING print()
                    print(str + (id ? " #" + id : "") + ": " + message);
                }
                
                // dispatch a LogAsEvent event for others to listen to
                $(this).trigger(evt);
            }
            
        }
    };
    

    // events dispatched
    LogAs.LOG_AS_EVENT = "log_as_event";
    
    // log levels
    LogAs.NONE      = 0;            // don't log
    LogAs.SEVERE    = 1;            // show only severe logs
    LogAs.WARNING   = 2;            // show severe and warning logs
    LogAs.INFO      = 3;            // show severe, warning and info logs
    LogAs.DEBUG     = 4;            // show all logs
    
    // singleton
    LogAs.instance = new LogAs();
    
    // static calls
    LogAs.severe  = function(message, id)   {   LogAs.instance.log(LogAs.SEVERE, message, id);  }
    LogAs.warning = function(message, id)   {   LogAs.instance.log(LogAs.WARNING, message, id);     }
    LogAs.info    = function(message, id)   {   LogAs.instance.log(LogAs.INFO, message, id);    }
    LogAs.debug   = function(message, id)   {   LogAs.instance.log(LogAs.DEBUG, message, id);   }
    
    window.LogAs = LogAs;
}(window));